/**
 * 
 */
package org.stars.database.connectionpool;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.stars.database.DatabaseInformation;
import org.stars.database.DatabaseType;
import org.stars.util.StringUtility;

/**
 * @author Francesco Benincasa (908099)
 * 
 */
public abstract class ConnectionHelper {

	/**
	 * Logger
	 * 
	 * @return logger
	 */
	protected static Log getLog() {
		return LogFactory.getLog(ConnectionHelper.class);
	}
	/**
	 * Crea una connessione al volo. La connessione è diretta e non viene
	 * inserita in alcun connection pool.
	 * 
	 * @param connectURI
	 *            uri del database
	 * @param username
	 *            username
	 * @param password
	 *            password
	 * @param driver
	 * @return connessione
	 * @throws Exception
	 *             in caso di errore
	 */
	public static Connection createConnection(String connectURI,
			String username, String password, String driver) throws Exception {
		Log log=getLog();
		try {
			driver = StringUtility.nvl(driver);
			username = StringUtility.nvl(username);
			password = StringUtility.nvl(password);
			Class.forName(driver);
			Connection con = DriverManager.getConnection(connectURI, username,
					password);
			log.debug("Creata connessione singola database " + connectURI);
			return con;
		} catch (Exception e) {
			log.fatal("Impossibile aprire la connessione " + connectURI
					+ " con driver " + driver + " Eccezione: " + e.toString()
					+ " " + e.getMessage());
			throw (e);
		}
	}
	
	/**
	 * Get database product name and version information. This method calls 4
	 * methods (getDatabaseMajorVersion(), getDatabaseMinorVersion(),
	 * getDatabaseProductName(), getDatabaseProductVersion()) to get the
	 * required information and it represents the information as XML.
	 * 
	 * @param conn
	 *            the Connection object
	 * @return database product name and version information as an XML document
	 *         (represented as a String object).
	 */
	public static DatabaseInformation getDatabaseInformation(Connection conn) throws Exception {
		DatabaseInformation ret = new DatabaseInformation();
		try {
			DatabaseMetaData meta = conn.getMetaData();
			if (meta == null) {
				return ret;
			}

			// Oracle (and some other vendors) do not
			// support some of the following methods
			// (such as getDatabaseMajorVersion() and
			// getDatabaseMajorVersion()); therefore,
			// we need to use a try-catch block.
			try {
				int majorVersion = meta.getDatabaseMajorVersion();
				ret.setMajorVersion(String.valueOf(majorVersion));
			} catch (Exception e) {

			}

			try {
				int minorVersion = meta.getDatabaseMinorVersion();
				ret.setMinorVersion(String.valueOf(minorVersion));
			} catch (Exception e) {

			}

			String productName = meta.getDatabaseProductName();
			String productVersion = meta.getDatabaseProductVersion();

			ret.setProductName(productName);
			ret.setProductVersion(productVersion);
			
			productName=StringUtility.nvl(productName).toLowerCase();
			
			// se arriva fino in fondo lo imposta a UNKNWON
			for(DatabaseType item: DatabaseType.values())
			{
				if (productName.contains(item.getStringToSearch()))
				{
					ret.setType(item);
					break;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Could not get the database information:" + e.toString());
		}
		return ret;
	}
}
